package com.jintoufs.service.baseinfo.impl;

import com.jhj.common.redis.JhjRedis;
import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.baseinfo.CurrencyExchangeDAO;
import com.jintoufs.domain.baseinfo.CurrencyExchange;
import com.jintoufs.domain.constants.RedisConstants;
import com.jintoufs.service.baseinfo.CurrencyExchangeService;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiaoemo
 */
@Repository
public class CurrencyExchangeServiceImpl implements CurrencyExchangeService {
    @Resource
    private CurrencyExchangeDAO currencyExchangeDAO;
    @Resource
    private JhjRedis jhjRedis;

    @Override
    public AjaxMsg insert(CurrencyExchange CurrencyExchange) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        Map map = new HashMap();
        map.put("scur", CurrencyExchange.getScur());
        map.put("tcur", CurrencyExchange.getTcur());
        List<Map<String,Object>> list = currencyExchangeDAO.queryByParams(map);
        String key = CurrencyExchange.getScur() + "-" + CurrencyExchange.getTcur();
        if (list.isEmpty()) {

            currencyExchangeDAO.insert(CurrencyExchange);
            jhjRedis.put(key,CurrencyExchange.getRate(),RedisConstants.DAY_SECONDS);
            ajaxMsg.setCode(AjaxMsg.SUCCESS);
        } else {
            CurrencyExchange.setId(Integer.parseInt(list.get(0).get("id").toString()));
            currencyExchangeDAO.updateByPrimaryKey(CurrencyExchange);
            jhjRedis.put(key,CurrencyExchange.getRate(),RedisConstants.DAY_SECONDS);
            ajaxMsg.setCode(AjaxMsg.SUCCESS);
            ajaxMsg.setMessage("对象已经更新");
        }
        return ajaxMsg;
    }

    @Override
    public void updateByPrimaryKey(CurrencyExchange CurrencyExchange) {
        currencyExchangeDAO.updateByPrimaryKey(CurrencyExchange);
    }

    @Override
    public void deleteByPrimaryKey(Integer id) {
        currencyExchangeDAO.deleteByPrimaryKey(id);
    }

    @Override
    public List<Map<String, Object>> getByFilter(QueryFilters filters) {
        return currencyExchangeDAO.getByFilter(filters);
    }

    @Override
    public Double queryRateByParams(Map<String, Object> params) {
        List<Map<String, Object>> list = currencyExchangeDAO.queryByParams(params);
        if (list.isEmpty()) {
            return 0D;
        } else {
            if (list.get(0).get("rate") != null) {
                return Double.parseDouble(list.get(0).get("rate").toString());
            }
        }
        return null;
    }

    @Override
    public Double queryRate(String scur, String tcur) {
        String key = scur + "-" + tcur;
        if (jhjRedis.get(key) != null) {
            Double rate = Double.parseDouble(jhjRedis.get(key).toString());
            return rate;
        }
        Map currencyParams = new HashMap();
        //scur:源币种 tcur：目标币种
        currencyParams.put("scur", scur);
        currencyParams.put("tcur", tcur);
        Double rate = queryRateByParams(currencyParams);
        jhjRedis.put(key, rate, RedisConstants.DAY_SECONDS);
        return rate;
    }

    @Override
    public List<Map<String, Object>> queryByParams(Map<String, Object> params) {
        return currencyExchangeDAO.queryByParams(params);
    }

    @Override
    public List<CurrencyExchange> queryByCurrencyExchange(Map<String, Object> params) {
        return currencyExchangeDAO.queryByCurrencyExchange(params);
    }

    @Override
    public CurrencyExchange selectByPrimaryKey(Integer id) {
        return currencyExchangeDAO.selectByPrimaryKey(id);
    }
}
